package com.mushuilingfeng.ddd.user.service.impl;

import com.mushuilingfeng.ddd.framework.event.IDomainEventPublisher;
import com.mushuilingfeng.ddd.user.bo.UserRegisterBO;
import com.mushuilingfeng.ddd.user.domain.base.BusinessException;
import com.mushuilingfeng.ddd.user.domain.base.DomainArgumentException;
import com.mushuilingfeng.ddd.user.domain.base.Ii8nCode;
import com.mushuilingfeng.ddd.user.domain.base.VerifyCodeType;
import com.mushuilingfeng.ddd.user.domain.base.service.VerifyCodeService;
import com.mushuilingfeng.ddd.user.domain.user.entity.RegisterInviteCode;
import com.mushuilingfeng.ddd.user.domain.user.entity.User;
import com.mushuilingfeng.ddd.user.domain.user.entity.UserLoginAccount;
import com.mushuilingfeng.ddd.user.domain.user.repository.IRegisterInviteCodeRepository;
import com.mushuilingfeng.ddd.user.event.InviteRegisterEvent;
import com.mushuilingfeng.ddd.user.service.IUserRegisterService;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

/**
 * @author
 * @date 2024/9/29
 */
@Service
@AllArgsConstructor
@Slf4j
public class UserRegisterServiceImpl implements IUserRegisterService {
    private final VerifyCodeService verifyCodeService;
    private final IRegisterInviteCodeRepository inviteCodeRepository;
    private final IDomainEventPublisher eventPublisher;
    
    @Override
    public void sendRegisterVerifyCode(String tel) {
        verifyCodeService.sendVerifyCode(tel, VerifyCodeType.REGISTER);
    }
    
    private User createAndSaveUser(UserRegisterBO bo) {
        User user = new User();
        user.setTelephone(bo.getTel());
        UserLoginAccount loginAccount = new UserLoginAccount();
        loginAccount.setLoginAccount(bo.getTel());
        loginAccount.setLoginPassword(bo.getPassword());
        user.setLoginAccount(loginAccount);
        user.setNickName(bo.getTel());
        user.saveNew();
        return user;
    }
    
    @Override
    public void register(UserRegisterBO bo) {
        // 验证验证码
        if (!verifyCodeService.verifyCode(bo.getTel(), VerifyCodeType.REGISTER, bo.getVerifyCode())) {
            throw new DomainArgumentException(Ii8nCode.VERIFY_CODE_ERROR);
        }
        // 注册用户
        createAndSaveUser(bo);
    }
    
    @Override
    public void inviteRegister(UserRegisterBO bo, String inviteCode) {
        // 验证邀请码是否正确
        RegisterInviteCode registerInviteCode = inviteCodeRepository.findByInviteCode(inviteCode);
        if (null == registerInviteCode) {
            throw new BusinessException(Ii8nCode.INVITE_CODE_ERROR);
        }
        registerInviteCode.checkCanUse();
        // 注册用户
        User newUser = createAndSaveUser(bo);
        // 核销邀请码
        registerInviteCode.use(newUser.getId());
        InviteRegisterEvent inviteRegisterEvent = new InviteRegisterEvent(registerInviteCode.getOwnerUserId(), registerInviteCode.getRegisterUserId());
        // 发送事件：邀请注册成功
        eventPublisher.publish(inviteRegisterEvent);
    }
    
}
